//============================================================================
// Name        : RedisConnectPoolTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "RedisConnectPool.h"
 #include <unistd.h>
#include <sys/time.h>
#include <string>
#include <sstream>
#include <signal.h>
#include <stdint.h>
#include "string.h"
#include "stdlib.h"

using namespace std;

int iNum = 0;
template <class T>
string num2String(T num)
{
	ostringstream stream;

	stream << num;

	return stream.str();
}

bool bExit = false;

void Exit(int iSignal)
{
	bExit = true;
}

void * ThreadTest(void * ptr)
{
	for(;;)
	{
		struct timeval tv_begin, tv_end;
		gettimeofday(&tv_begin, NULL);
		CRedisManager *redis = CRedisConnectPool::Instance()->GetRedis();
		gettimeofday(&tv_end, NULL);
		if(redis != NULL)
			cout <<" result : "<< tv_end.tv_usec - tv_begin.tv_usec <<endl;
		else
			cout << "redis get Error.!" << endl;

		redis->Set("cxw", "name" + num2String<int>(iNum ++ ));
		sleep(2);
		CRedisConnectPool::Instance()->ReleaseRedis(redis);

		if(bExit)
			break;
	}

	return 0;
}

using namespace std;

struct ST_Test
{
	struct ST_V
	{
		int ia;
		int ib;
	};
	//int iNum;
	//int16_t i16Small;
	//int8_t i8Type;
	//int iPrice;
	vector<ST_V> vnTemp;

	ST_Test()
	{
		memset(this, 0x0, sizeof(ST_Test));
	}

	ST_Test(int iNum, int16_t i16Small, int8_t i8Type, int iPrice)
	{
		/*this->iNum = iNum;
		this->i16Small = i16Small;
		this->i8Type = i8Type;
		this->iPrice = iPrice;*/
	}
};

int main() {
	signal(SIGINT, Exit);


	int nLen = sizeof(ST_Test);
	CRedisConnectPool::Instance()->SetInitConNum(10);
	CRedisConnectPool::Instance()->SetMaxConNum(1500);
	if(!CRedisConnectPool::Instance()->Open())
	{
		cout <<"redis connect pool creat error." <<endl;
		return -1;
	}

	std::vector<void *> vpValues;
	std::vector<int> vpValuesLen;
	std::vector<string> vstrKeys;
	for(int i = 0; i < 5; i ++)
	{
		ST_Test stTest(i, 12, 2, i);

		void *pData = NULL;
		pData = malloc(sizeof(ST_Test));
		memset(pData, 0x0, sizeof(ST_Test));
		memcpy(pData, (void *)&stTest, sizeof(ST_Test));
		vpValues.push_back(pData);
		vpValuesLen.push_back(sizeof(ST_Test));

		string strData = "";
		strData = "testKey" + num2String<int>(i);

		vstrKeys.push_back(strData);
	}

	CRedisManager * pRedis = CRedisConnectPool::Instance()->GetRedis();

	pRedis->SetHashKeyValueBinary("test", vstrKeys, vpValues, vpValuesLen);

	void *pRData = NULL;
	std::vector<string> vstrRKeys;
	std::vector<void *> vpRValues;

	pRedis->IsExistsKey("test");
	pRedis->GetHashAllDataBinary(vstrRKeys, vpRValues, "test1");

	ST_Test * stRTest;
	for(size_t i = 0; i < vpRValues.size(); i ++)
	{
		stRTest = (ST_Test *)vpRValues.at(i);
	}

	for(size_t i = 0; i < vpRValues.size(); i ++)
	{
		free(vpRValues.at(i));
	}
/*
	vector<pthread_t> vId;
	int iThreadNum = 1600;
	for(int i = 0; i < iThreadNum; i ++)
	{
		pthread_t id;
		pthread_create(&id, NULL, ThreadTest, NULL);
		vId.push_back(id);
	}
	for(int i = 0; i < iThreadNum; i ++)
	{
		pthread_join(vId.at(i), NULL);
	}
*/



/*	CRedisManager *pRedis = CRedisConnectPool::Instance()->GetRedis();
	pRedis->Set("name", "cxw");
	CRedisConnectPool::Instance()->ReleaseRedis(pRedis);*/

	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	return 0;
}
